package com.dms.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dms.common.QueryPageParam;
import com.dms.common.Result;
import com.dms.entity.User;
import com.dms.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dms
 * @since 2023-02-05
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    //查询是否账号存在
    @GetMapping("/findByNo")
    public Result findByNo(@RequestParam String no){
        List list= userService.lambdaQuery().eq(User::getNo,no).list();
        return list.size()>0?Result.suc(list):Result.fail();
    }
    //登录
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        List list= userService.lambdaQuery().eq(User::getNo,user.getNo())
                .eq(User::getPassword,user.getPassword()).list();

        if(list.size()>0){
            User suser1=(User)list.get(0);
//            System.out.println(suser1);
            HashMap res=new HashMap();
            res.put("user",suser1);
            return Result.suc(res);
        }
        return Result.fail();
    }
    //修改
    @PostMapping("/update")
    public Result update(@RequestBody User user){
        return userService.updateById(user)?Result.suc():Result.fail();
    }
    //删除
    @GetMapping("/del")
    public Result del(@RequestParam int id){
        return userService.removeById(id)?Result.suc():Result.fail();
    }
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody User user){

        return userService.save(user)?Result.suc():Result.fail();
    }

    //精准查询并分页
    @PostMapping("/listpagecc1")
    public Result listpagecc1(@RequestBody QueryPageParam query){
        HashMap param =query.getParam();
        String no=(String) param.get("no");
        String name=(String) param.get("name");


        Page<User> page=new Page();

        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());
        LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(no) && !"null".equals(no)) {
            lambdaQueryWrapper.like(User::getNo, no);
        }
        if(StringUtils.isNotBlank(name) && !"null".equals(name)) {
            lambdaQueryWrapper.like(User::getName, name);
        }

        IPage result= userService.pageCC(page,lambdaQueryWrapper);
        System.out.println("total="+result.getTotal());
        System.out.println(result.getRecords());
        return Result.suc(result.getRecords(),result.getTotal());
    }
}
